package wyp.library.ui.vc;

import java.util.ArrayList;

import wyp.library.ui.fragment.WypFragment;
import wyp.library.ui.view.WypGroupView;
import wyp.library.ui.view.WypGroupView.WypGroupViewDelegate;
import wyp.library.ui.view.WypBarItem;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class WypTabInicatorViewController implements OnPageChangeListener, WypGroupViewDelegate {

	public static final String TAG = WypTabInicatorViewController.class.getSimpleName();

	public interface WypTabInicatorDelegate {
		public void onSelection_WypTabInicatorDelegate(int _index, WypBarItem _item);
		public void onInit_WypTabInicatorDelegate(WypBarItem _item);
	}

	public int lineColor = Color.GRAY;
	private LinearLayout mMainLayout;
	private FragmentManager mFragmentManager;
	private MyAdapter mAdapter;
	private ArrayList<WypBarItem> mItemList;
	private ViewPager mViewPager;
	private WypTabInicatorDelegate mDelegate;

	public WypTabInicatorViewController(LinearLayout _linearLayout, FragmentManager _fragmentManager, WypTabInicatorDelegate _delegate, ViewPager _viewPager) {
		super();
		mMainLayout = _linearLayout;
		mMainLayout.setOrientation(LinearLayout.HORIZONTAL);
		mFragmentManager = _fragmentManager;
		mDelegate = _delegate;
		mViewPager = _viewPager;
		mViewPager.setOffscreenPageLimit(3);
		mViewPager.setOnPageChangeListener(this);
	}

	public void setCurrentItem(int _index) {
		if (mViewPager.getCurrentItem() == _index) {
			this._setHighlight(_index);
		} else {
			mViewPager.setCurrentItem(_index);
		}
	}

	private void _setHighlight(int _index) {
		for (int i = 0; i < this.mItemList.size(); i++) {
			if (i == _index) {
				this.mItemList.get(i).setHighlight();
			} else {
				this.mItemList.get(i).setNormal();
			}
		}
	}

	public void addTabItem(ArrayList<WypBarItem> _itemList) {
		mItemList = _itemList;
		int _size = mItemList.size();
		for (int i = 0; i < _size; i++) {
			mMainLayout.addView(mItemList.get(i));
			mItemList.get(i).setDelegate(this);
			if (i < _size - 1) {
				LinearLayout _line = new LinearLayout(mMainLayout.getContext());
				_line.setLayoutParams(new LayoutParams(1, -1));
				_line.setBackgroundColor(lineColor);
				mMainLayout.addView(_line);
			}
		}
		mAdapter = new MyAdapter(mFragmentManager);
		mViewPager.setAdapter(mAdapter);
	}

	// ------------------------- WypGroupViewDelegate -------------------------
	@Override
	public void onClick_WypGroupView(WypGroupView _view) {
		mViewPager.setCurrentItem(this.mItemList.indexOf(_view));
	}

	// ------------------------- OnPageChangeListener -------------------------
	@Override
	public void onPageSelected(int _position) {
		this._setHighlight(_position);
		if (this.mDelegate != null) {
			this.mDelegate.onSelection_WypTabInicatorDelegate(_position, this.mItemList.get(_position));
		}
	}

	@Override
	public void onPageScrollStateChanged(int _position) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	// ------------------------- MyAdapter -------------------------
	private class MyAdapter extends FragmentPagerAdapter {

		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int _position) {
			WypFragment __fragment = mItemList.get(_position).getOrCreateFragment();
			mDelegate.onInit_WypTabInicatorDelegate(mItemList.get(_position));
			return __fragment;
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

	}
}